﻿@{
    ViewBag.Title = "UpdateDriverCertificate";
    Layout = "~/Views/Shared/_BaseLayout.cshtml";
    ViewBag.First = "车辆管理";
    ViewBag.Second = "创建证书";
    var list = ViewData["type"] as IList<Sc.Domain.BaseItem>;
    var ent = ViewData["ent"] as Sc.Domain.DriverCertificate;
}
<script src="~/Content/laydate-v1.1/laydate/laydate.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        initSingleDate("txtStartTime", "YYYY/MM/DD");
        $(".select2").uedSelect({
            width: 247
        });
    });
    //司机选择
    function selectDriver() {
        layer.open({
            type: 2,
            title: "驾驶员选择",
            shade: [0],
            area: [($(window).width() - 20) + "px", ($(window).height() - 80) + "px"],
            content: ['@Url.Action("SelectDriver", "CarInfo")?r=1', 'yes']
        });
    }
    //司机选择后
    function sureDriver(list) {
        var txtDriver = $("#txtDriver");
        txtDriver.val(list[0].Name);
        txtDriver.attr("driverId", list[0].Id);
        layer.closeAll();
    }
    //数据保存
    function saveData(tag) {
        var txtCertificateCode = $("#txtCertificateCode").val();
        var selType = $("#selType").val();
        var txtStartTime = $("#txtStartTime").val();
        var txtUsefulLife = $("#txtUsefulLife").val();
        var txtScore = $("#txtScore").val();
        if (txtCertificateCode.length <= 0) {
            Tips("请输入证书编号", "txtCertificateCode");
            return false;
        }
        if (txtStartTime.length <= 0) {
            Tips("请选择有效期开始时间", "txtStartTime");
            return false;
        }
        if (txtUsefulLife.length <= 0) {
            Tips("有效年限不能为空", "txtUsefulLife");
            return false;
        }
        if (isNaN(txtUsefulLife)) {
            Tips("有效年限只能为数字", "txtUsefulLife");
            return false;
        }
        if (txtScore.length <= 0) {
            Tips("请输入年检考试得分", "txtScore");
            return false;
        }
        if (isNaN(txtScore)) {
            Tips("年检考试得分只能为数字", "txtScore");
            return false;
        }
        var ent = new Object();
        ent.Driver = $("#txtDriver").attr("driverId");
        ent.BaseItemId = selType;
        ent.Id = '@ent.Id';
        ent.CertificateCode = txtCertificateCode;
        ent.UsefulLife = txtUsefulLife;
        ent.Score = txtScore;
        ent.StartTime = txtStartTime;
        var tp = $("#tp").find("li");
        var paths = "";
        for (var i = 0; i < tp.length; i++) {
            if (i > 0) {
                paths += ",";
            }
            paths += tp[i].id;
        }
        var url = "@Url.Action("SaveData")";
        var load = loadTip("数据提交中，请稍候", 16, 0);
        $.post(url, { data: JSON.stringify(ent), fp: paths }, function (data) {
            if (data > 0) {
                if (tag == 1) {
                    layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () {
                        document.getElementById("formData").reset();
                        $("#tp").find("li").remove();
                    });
                } else {
                    layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () {
                        back();
                    });
                }
            }
        });
    }
    function back() {
        window.location.href = "@Url.Action("Index")";
    }
    //文件上传
    function fileUpload() {
        var load = loadTip('图片处理中，请稍后', 16, 0);
        $.ajaxFileUpload({
            url: "@Url.Action("FileUpload", "Base")",
            secureuri: false,
            fileElementId: "fl",
            type: 'POST',
            dataType: 'text',
            success: function (data) {
                var ul = document.getElementById("tp");
                var li = document.createElement("li");
                li.style.width = "140px";
                li.style.height = "140px";
                li.id = data;
                li.innerHTML = "<a><img width='120px'style='border-radius:5px' height='120px' src='/UploadFiles/CarImg/" + data + "' /></a><h2 style='line-height:130px;margin-left:-20px;color:red;cursor:pointer;' onclick='delPic(this,0);'>删除</h2>";
                ul.appendChild(li);
                layer.close(load);
            }
        });
    }
    //图片删除
    function delPic(obj,id) {
        layer.confirm("您确定要删除吗？", function () {
            if (id == 0) {
                $(obj).parent().remove();
                layer.closeAll();
            } else {
                var url = "@Url.Action("DeleteSysAttachments", "CarScrap")";
                var load = loadTip('附件删除中，请稍后', 16, 0);
                $.get(url, { id: id }, function (data) {
                    if (data > 0) {
                        $(obj).parent().remove();
                        layer.closeAll();
                    }
                });
            }
        });
    }
</script>
<div class="formbody">
    <div class="formtitle"><span>创建证书</span></div>
    <form method="post" enctype="multipart/form-data" class="jqtransform" id="formData" style="margin-top:-20px;">
        <table style="width:100%;line-height:48px;">
            <tr>
                <td><label>证书编号</label></td>
                <td>
                    <input type="text" class="dfinput" value="@ent.CertificateCode" id="txtCertificateCode" name="CertificateCode" />
                </td>
                <td><label>驾 驶 员</label></td>
                <td>
                    <input type="text" class="dfinput" driverid="@ent.Driver" value="@ent.Name" readonly="readonly" id="txtDriver" />
                    <input type="button" class="scbtn" onclick="selectDriver();" value="选择" />
                </td>
            </tr>
            <tr>
                <td><label>证书类型</label></td>
                <td>
                    <div class="vocation">
                        <select name="select" class="select2" id="selType">
                            @if (list != null)
                            {
                                foreach (var item in list)
                                {
                                    <option value="@item.Id" @(item.Id == ent.BaseItemId ? "selected" : "")>@item.Name</option>
                                }
                            }
                        </select>
                    </div>
                </td>
                <td><label>有效期开始时间</label></td>
                <td><input type="text" class="dfinput" value="@ent.StartTime.ToString("yyyy/MM/dd")" id="txtStartTime" readonly="readonly" /></td>
            </tr>
            <tr>
                <td><label>有效期年限</label></td>
                <td><input type="text" class="dfinput" id="txtUsefulLife" value="@ent.UsefulLife" /></td>

                <td><label>年检考试得分</label></td>
                <td><input type="text" class="dfinput" id="txtScore" value="@ent.Score" /></td>
            </tr>
        </table>
        <div class="formtitle"><span>证书照片</span></div>
        <div class="toolsli">
            <ul class="toollist" id="tp">
                @if (ent.AttactList != null)
                {
                    foreach (var item in ent.AttactList)
                    {
                        <li style="width:140px;height:140px">
                            <a><img width="120" style="border-radius:5px" height="120" src="/UploadFiles/CarImg/@item.FileUrl" /></a><h2 style="line-height:130px;margin-left:-20px;color:red;cursor:pointer;" onclick="delPic(this,@item.Id);">删除</h2>
                        </li>
                    }
                }
            </ul>
            <span class="tooladd" style="cursor:pointer" onclick="fl.click();"><img src="~/Content/images/add.png" alt="添加" /></span>
        </div>
        <input type="file" onchange="fileUpload()" style="display:none" name="fl" id="fl" />
        <input type="button" style="margin-left:20px;" class="btn" onclick="saveData(2);" value="保存退出" />
        <input type="button" class="btn" onclick="back();" value="返 回" />
    </form>
</div>

